package com.astrocsr.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblGroup;

@Repository("groupDAO")
public class GroupDAO extends BaseHelperDAO {

	public static final String NAME 	 	= "name";
	public static final String GROUP_CODE 	= "groupCode";
	public static final String PARENT_ID 	= "parentId";

	public List<TblGroup> findByGroupCode(Object groupCode) throws Exception {
		return findByProperty(GROUP_CODE, groupCode);
	}
	
	public List<TblGroup> findByName(Object name) throws Exception {
		return findByProperty(NAME, name);
	}

	public List<TblGroup> findByParentId(Object parentId) throws Exception {
		return findByProperty(PARENT_ID, parentId);
	}

	@SuppressWarnings("unchecked")
	public List<TblGroup> findByProperty(String propertyName, Object value)
			throws Exception {

		try {

			String queryString = "from TblGroup as model where model."
					+ propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TblGroup> listTblGroup() throws Exception {
		List<TblGroup> result = null;

		String queryString = "from TblGroup"; // Hibernate Query Language
		result = getCurrentSession().createQuery(queryString).list();

		return result;
	}

	/**
	 * Insert customer into database
	 */
	public void addTblGroup(TblGroup entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}
}